define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;
    require("wookmark");
    var videoItem = require("./items/videoItem.html");
    var videoBlockTpl = require("./blocks/videoBlockTpl.html");

    var render_list = template.compile(videoBlockTpl);
    var render_item = template.compile(videoItem);


    var composeVideoView = require('./composes/composeVideoView.html');
    var videoView = require("./preview/videoView.html");
    var videofooter = require("./footers/videofooter.html")

    var emptyHtml = require('./emptys/video.html')
    var videofiles = function(element, layout) {
        this.element = $(element);
        this.layout = layout;
        this.selector = ".js_video_container .video_unit:last";
        this.type = window.message_config.files_type_ids[3];
        this.init();
    }
    videofiles.prototype.init = function() {
        this.element.empty();
        this.events();
        this.selectView();
        this.fileModel = {
            FileID: 0,
            Video: {},
            file_name: ''
        }
        this.model = {}
    };
    videofiles.prototype.templateData = function() {
        return {
            model: this.model,
            Video: this.fileModel.Video,
            file_name: this.fileModel.file_name,
            app: window.message_config.app
        }
    };
    videofiles.prototype.validation = function() {
        if ($.trim(this.fileModel.Video.path).length==0) {
            config.msg.info("请上传视频")
            return false;
        };
        if ($.trim(this.fileModel.Video.title).length==0) {
            config.msg.info("请输入标题")
            return false;
        };
        return true;
    };
    videofiles.prototype.selectData = function(data) {
        var _self = this;
        $(".js_add_message").off("click").removeAttr('disabled').bind('click', function(event) {
            _self.generate(data)
        });
    }
    videofiles.prototype.events = function() {
        var _self = this;
        $(document).off('click.js_video_container').on("click.js_video_container", ".js_video_container .video_unit", function() {
            var $this = $(this),
                $fileId = $this.closest('li').data("fileId");
            $(".js_video_container .video_unit").removeClass('selected');
            $this.addClass('selected').siblings().removeClass('selected');
            $fileId && _self.selectData(window.files[_self.type][$fileId]);

        })
    };
    videofiles.prototype.selectView = function() {
        var $footer = $(videofooter);
        var _self = this;
        var list_data = {};
        $(".js_new_message", $footer).on("click", function() {
            _self.generate();
        })
        this.layout.setFooter($footer);
    }
    videofiles.prototype.generate = function(data) {
        var _self = this;
        if (data) {
            if (data.file_id) {
                this.fileModel.FileID = data.file_id;
            };

            this.fileModel.Video = data.Video;
            if (data.size) {
                this.fileModel.file_name = "{0} ({1})".format(data.file_name, data.size);
            } else {
                this.fileModel.file_name = data.file_name;
            }

        };
        var warp = template.compile(composeVideoView)(this.templateData());
        var $warp = $(warp);


        var preview = template.compile(videoView)(this.templateData());
        this.generateChange($warp);
        this.layout.setWarp($warp);
        this.layout.showPreview();
        this.layout.setPreview(preview);
        this.layout.setMassfooter(this);
        this.layout.blcokDestroy()
    }
    videofiles.prototype.sync = function($warp) {
        var _self = this;
        _self.fileModel.Video.title = $(".js_Video_title").val();
        _self.fileModel.Video.description = $(".js_Video_description").val();
        var preview = template.compile(videoView)(this.templateData());
        this.layout.setPreview(preview);

    }
    videofiles.prototype.generateChange = function($warp) {
        var _self = this;
        $(".js_Video_title", $warp).add(".js_Video_description", $warp).on("keyup", function() {
            _self.sync()
        })
        // .on("blur", function() {
        //     _self.sync();
        // })
        $(".group_item_close",$warp).on("click",function  () {
             $(".js_upload_complete").hide();
             _self.fileModel.Video.path=config.empty;
             _self.fileModel.Video.thumb_path=config.empty;
             _self.sync();
        })
        $(".js_upload", $warp).uploader({
            url: window.message_config.upload_path,
            data: {
                type: _self.type,
                data: window.message_config.upload_params,
                FileID: _self.fileModel.FileID
            },
            FilesAdded: function(up, files) {
                $(".js_upload_complete").hide();
                $(".js_upload_progress").show(); 
                var file = files[0];
                $(".video_upload_filename").html("{0} ({1})".format(file.name, plupload.formatSize(file.size)))
            },
            UploadProgress: function(up, file) {
                var percent = file.percent + "%";
                $(".js_process").width(percent)

            },
            FileUploaded: function(up, file, res) {
                if ($.isPlainObject(res)) {
                    _self.fileModel.Video.thumb_path = res.Video.thumb_path;
                    _self.fileModel.Video.path = res.Video.path;
                    _self.fileModel.file_size=res.Video.file_size;
                    _self.fileModel.FileID = res.file_id;
                    _self.fileModel.file_name = "{0} ({1})".format(res.file_name, res.size);

                } else {
                    up.files.removeFile(file);
                    config.msg.info(res || config.lang.uplodError);
                }
            },
            UploadComplete: function(up, files) {
                _self.generate(_self.templateData());
            }
        }, "video")

    }
    videofiles.prototype.list = function(data) {
        var sp = $('ul:last', this.element);
        if (sp.length) {
            sp.append(render_item({
                list: data.file_item
            }))
        } else {
            if (data.last_file_item.length) {
                this.element.append(render_list({
                    file_items: render_item({
                        list: data.file_item
                    })
                }));
            } else {
                this.element.html(emptyHtml)
            }

        }
        $(".js_video_container li.tile").wookmark({
            align: "left",
            autoResize: !0,
            container: $(".js_video_container .js_items"),
            offset: 20
        })


    };

    return videofiles;

})
